package com.ziroom.finance.rabbitmq.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

/**
 * ip判断
 *
 * @Author liyb58
 * @Date 2016/11/13 19:31
 * @return
 */
public class IpUtil {

    private final static Logger logger = LoggerFactory.getLogger(IpUtil.class);

    public static String ENVIROMENT = "";

    /**
     * 获取ip
     * <p>
     * <b>注意：</b><br>
     * </p>
     * <p>
     * <b>变更记录：</b><br>
     * </p>
     *
     * @return
     * @throws Exception
     * @author: gaojk
     * @date:2015年5月12日 上午9:19:51
     * @since 1.0.0
     */
    public static Boolean getIpFlg(String key) {

        //获取主机ip
        String ip = IpUtil.getHostIP();
        logger.info("ip 为：" + ip);

        //获取轮询ip
        String[] ips = PropertiesUtil.getPropValAsString(key).split(",");

        boolean isNext = false;
        for (String _ip : ips) {
            if (_ip.equals(ip)) {
                isNext = true;
                break;
            }
        }
        if (!isNext) {
            logger.info("您的ip地址:" + ip
                    + "不是系统配置的,所以无法进行进件,如果想进件,请修改/resources/application.properties里的" + key + "键值!");
        }
        return isNext;
    }

    /**
     * 获取主机IP
     * <p>
     * <p>
     * <b>注意：</b><br>
     * </p>
     * <p>
     * <b>变更记录：</b><br>
     * </p>
     *
     * @return
     * @author: gaojk
     * @date:2015年5月12日 上午9:22:07
     * @since 1.0.0
     */
    public static String getHostIP() {
        String IFCONFIG = null;
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf
                        .getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()
                            && !inetAddress.isLinkLocalAddress()
                            && inetAddress.isSiteLocalAddress()
                            && inetAddress.getHostAddress().toString().indexOf("10") >= 0) {
                        IFCONFIG = inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException ex) {
        }
        return IFCONFIG;
    }

    /**
     * 获取请求IP地址
     * <p><b>注意：</b><br>
     * </p>
     * <p>
     * <b>变更记录：</b><br>
     * </p>
     *
     * @param request
     * @return
     * @author: gaojk
     * @date:2015年11月24日 下午6:59:53
     * @since 1.0.0
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
                //根据网卡取本机配置的IP  
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ipAddress = inet.getHostAddress();
            }
        }
        //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割  
        if (ipAddress != null && ipAddress.length() > 15) { //"***.***.***.***".length() = 15
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }

}
